package com.hbnu.iostream;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 * 需求：通过字符输出流向指定文件中写入内容
 *
 * @author kittychen
 * @since 2025-10-13 16:24
 */
public class WriterDemo {
    public static void main(String[] args) {
        // 1、准备文件对象
        String path = "C:" + File.separator + "Users" + File.separator + "Administrator"
                + File.separator + "Desktop" + File.separator + "kittychen"
                + File.separator + "class2311" + File.separator + "writer.txt";
        File file = new File(path);

        Writer writer = null;
        try {
            writer = new FileWriter(file, true);  // true表示追加写入

            String content = "Hello World!";
            writer.write(content);

            System.out.println("文件写入成功！！");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
